package edu.egypt.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.bcel.internal.generic.Type;
import com.google.gson.Gson;

import edu.egypt.helpers.PermissionsDbOperations;
import edu.egypt.model.Permission;
import edu.egypt.model.perfather;

/**
 * Servlet implementation class putJsonIntoDB
 */
public class putJsonIntoDB extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public putJsonIntoDB() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int Rid = Integer.parseInt(request.getParameter("id"));
		//System.out.println(Rid);
		ArrayList<Permission> allPermissions = new ArrayList<>();
		String json = request.getParameter("data");
		Gson g = new Gson();
		java.lang.reflect.Type token = new TypeToken<ArrayList<Permission>>() {}.getType();
		allPermissions = g.fromJson(json,token);
		for(int i=0;i<allPermissions.size();i++)
		{
			if(allPermissions.get(i).getAssigned().equals("YES"))
		    PermissionsDbOperations.getSingletonInstance().update(Rid,allPermissions.get(i).getId());
			else
			PermissionsDbOperations.getSingletonInstance().update2(Rid,allPermissions.get(i).getId());
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
